# GF_PY3_CLASS_Text_Operation.py
# Create by GF 2025-04-16 20:34

class Text_Operation(object):

    def Text_Opr_Content_in_Mark_Extract(self, Text:str, Mark_Start:str, Mark_End:str, With_Mark:int=1) -> str:

        # Text Operation - Extract The Content in Mark
        #
        # >>> Text = "Here is The Code:\n\n<CODE_START>\nDESCRIBE example_table;\n<CODE_END>"
        # >>> print(Text)
        # Here is The Code:
        #
        # <CODE_START>
        # DESCRIBE example_table;
        # <CODE_END>
        # >>>
        # >>> Content_in_Mark = Text_Opr_Content_in_Mark_Extract(Text, "<CODE_START>\n", "<CODE_END>", 1)
        # >>> print(Content_in_Mark)
        # <CODE_START>
        # DESCRIBE example_table;
        # <CODE_END>
        # >>> Content_in_Mark = Text_Opr_Content_in_Mark_Extract(Text, "<CODE_START>\n", "<CODE_END>", 0)
        # >>> print(Content_in_Mark)
        # DESCRIBE example_table;
        
        Capture:list = [0, 0]
        Founded:list = [0, 0]
        Content_in_Mark:str = ''
        # ..............................................
        for i in range(0, len(Text), 1):
            Slice_Idx_A = i
            Slice_Idx_B = i + len(Mark_Start)
            if (Text[Slice_Idx_A:Slice_Idx_B] == Mark_Start):
                Capture[0] = Slice_Idx_A
                Founded[0] = 1
                break
        # ..............................................
        if (Founded[0] == 0):  # If The "Mark Start" is not Found, Return "Empty Char"
            return Content_in_Mark
        # ..............................................
        for i in range((Capture[0] + len(Mark_Start)), len(Text), 1):
            Slice_Idx_A = i
            Slice_Idx_B = i + len(Mark_End)
            if (Text[Slice_Idx_A:Slice_Idx_B] == Mark_End):
                Capture[1] = Slice_Idx_B
                Founded[1] = 1
                break
        # ..............................................
        if (With_Mark == 0):
            Capture[0] = Capture[0] + len(Mark_Start)
            Capture[1] = Capture[1] - len(Mark_End)
        # ..............................................
        Content_in_Mark = Text[Capture[0]:Capture[1]]
        # ..............................................
        return Content_in_Mark

    def Text_Opr_Content_in_Mark_Delete(self, Text:str, Mark_Start:str, Mark_End:str, With_Mark:int=1) -> str:

        # Text Operation - Delete The Content in Mark
        #
        # >>> Text = "Here is The Code:\n\n<CODE_START>\nDESCRIBE example_table;\n<CODE_END>"
        # >>> print(Text)
        # Here is The Code:
        #
        # <CODE_START>
        # DESCRIBE example_table;
        # <CODE_END>
        # >>>
        # >>> New_Text = Text_Opr_Content_in_Mark_Delete(Text, "<CODE_START>\n", "<CODE_END>", 1)
        # >>> print(new_Text)
        # Here is The Code:
        #
        # >>> New_Text = Text_Opr_Content_in_Mark_Delete(Text, "<CODE_START>\n", "<CODE_END>", 0)
        # >>> print(New_Text)
        # Here is The Code:
        #
        # <CODE_START>
        # <CODE_END>

        Capture:list = [0, 0]
        # ..........................................
        for i in range(0, len(Text), 1):
            Slice_Idx_A = i
            Slice_Idx_B = i + len(Mark_Start)
            if (Text[Slice_Idx_A:Slice_Idx_B] == Mark_Start):
                Capture[0] = Slice_Idx_A
                break
        # ..........................................
        for i in range((Capture[0] + len(Mark_Start)), len(Text), 1):
            Slice_Idx_A = i
            Slice_Idx_B = i + len(Mark_End)
            if (Text[Slice_Idx_A:Slice_Idx_B] == Mark_End):
                Capture[1] = Slice_Idx_B
                break
        # ..........................................
        if (With_Mark == 0):
            Capture[0] = Capture[0] + len(Mark_Start)
            Capture[1] = Capture[1] - len(Mark_End)
        # ..........................................
        New_Text = Text.replace(Text[Capture[0]:Capture[1]], '')
        # ..........................................
        return New_Text

    def Text_Opr_Loop_Read_by_Word_Num(self, Text:str, Begin_Index:int, Word_Num:int) -> tuple:

        # Text Operation - Loop Read by Word Number
        #
        # >>> Text = "A string is a string of characters consisting of numbers, letters, and underscores."
        # >>> Text_Readed_1 = Text_Opr_Loop_Read_by_Word_Num(Text,   0, 10)
        # >>> print(Text_Readed_1)
        # (10, "A string i")
        # >>> Text_Readed_2 = Text_Opr_Loop_Read_by_Word_Num(Text,  10, 20)
        # >>> print(Text_Readed_2)
        # (30, "s a string of charac")
        # >>> Text_Readed_3 = Text_Opr_Loop_Read_by_Word_Num(Text, 107, 10)
        # >>> print(Text_Readed_3)
        # (None, '')

        Text_Total_Word_Num = len(Text)
        # ..............................................
        Bgn_Idx = Begin_Index
        End_Idx = Bgn_Idx + Word_Num # Python 3 字符串切片, 含开始索引, 不含结束索引
        # ..............................................
        if (End_Idx >= Text_Total_Word_Num):
            End_Idx = Text_Total_Word_Num
        # ..............................................
        Readed_Text_Slice = Text[Bgn_Idx:End_Idx]
        # ..............................................
        Bgn_Idx = End_Idx           # 设置 Next Loop 开始索引
        # ..............................................
        if (End_Idx >= Text_Total_Word_Num):
            Bgn_Idx = None
        # ..............................................
        return (Bgn_Idx, Readed_Text_Slice)

# EOF Signed by GF.
